package solver;

import java.lang.reflect.Method;

/**
 * Abstract class for solve method
 * @author tigerzhong
 *
 */
public abstract class Searcher {
    /**
     * update the parameters
     * @param parameter
     * @param dev
     * @return new parameters
     */
    public abstract double update(double parameter, double dev);
    
    /**
     * Reflection, get an object of searcher
     * @param searcherName
     * @param methodList
     * @param valuesList
     * @return Searcher
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static Searcher forName(String searcherName, String methodList[], String valuesList[]) throws Exception{
	Object ob = Class.forName(searcherName).newInstance();
	Class ownerClass = ob.getClass();
	int len = methodList.length;
	Class[] argsClass = new Class[1];
	Object[] args = new Object[1];
	for(int i=0;i<len;i++){
	    args[0] = Double.parseDouble(valuesList[i]);
	    argsClass[0] = args[0].getClass();
	    Method method = ownerClass.getMethod(methodList[i], argsClass);
	    method.invoke(ob, args);
	}
	return (Searcher)ob;
    }
}
